<template>
	<view class="content">
		<!-- 待用活动表单1 -->
		<active-form :formDate="formDate1" @input-val="inputVal1" :head="head" >
		</active-form>
		<wyb-popup  ref="popup" style="text-align: center;" type="center" height="1000" width="550"  :showCloseIcon="true">
	 	    <view class="popup-content">
				<!--这个源单数量弹出框是欧骏的-->
		       <h5 style="font-size:20px;">源单数量计算方式</h5>
			   <br><span style="color: #9a9a98;">最小包装数量：{{smallqty}}</span>
			<!--  <input :value="smallqty" style="color: #9a9a98;" type="number"  disabled="true"  placeholder="请输入最小包装数量" /> -->
			  <span class="lab">总数</span>
			   <input :style="color"  class="inp" @input="input(2)"   v-model="sumqty" type="number"  placeholder="请输入总数" />
			   <span class="lab">包数</span><br>
			    <input :style="color" class="inp"  @input="input(1)" :focus="true"   v-model="countqty"  type="number"  placeholder="请输入包数" />
			   <button :style="fontcolor" @click="close">计算完成</button>
		    </view> 
		</wyb-popup>
		<view class="box">
		<view class="buttonBox"
		 :style="[{'margin-top': inputOffsetBottom > 0 ? '-'+inputOffsetBottom + 'px' : '0'},bj]"
		 @click="addrow">
			添加行
			</view>
		<view class="buttonBox" 
		:style="[{'margin-top': inputOffsetBottom > 0 ? '-'+inputOffsetBottom + 'px' : '0'},bj]"
         @click="submit1">
			编辑完成
		</view>
		</view>
	</view>
	</view>
</template>

<script>
	import wybPopup from '@/components/wyb-popup/wyb-popup.vue'
	import activeForm from './../../components/active_form.vue';
	export default {
		components: {
			activeForm,
			wybPopup
		},
		data() {
			return {
				formDate1: [],
				srcInfos: [],
				qty: 0,
				head:[],
				smallqty:0,
				countqty:1,
				sumqty:0,
				obj:{},
			    inputOffsetBottom:"",
				fontcolor:{
				color:'#FFFFFF',
				MarginTop: '5px',
				backgroundColor:this.$kdup.color,
				},
				bj:{
				backgroundColor:this.$kdup.color,
				},
				color:{
					border:' 1px dashed'+this.$kdup.color,
				}
			}
		},
		onLoad(e) {
			uni.setNavigationBarColor({
				frontColor: "#ffffff",	//文字颜色
				backgroundColor: this.$kdup.color	//底部背景色
			})
			//获取上个页面的源单
			var obj = JSON.parse(e.srcInfos);
			this.head = JSON.parse(e.head);
			//获取上个页面的数量
			this.qty = e.qty;
			this.$kdup.index = 0;
			for (var i = 0; i < obj.length; i++) {
				this.$kdup.index++;
				this.formDate1.push({
					placeholder: "请输入源单" + this.$kdup.index, //提示
					label: "源单" + this.$kdup.index, //标题
					index: this.$kdup.index,
					type: "text", //类型ffieldtype
					disabled: true, //是否启用
					maxlength: "-1", //输入长度
					property: {
						fishidden: 0,
						ffieldtype: 55,
						ffilter: "",

					},
					key: "sourcekey", //唯一标识
					rules: {
						flag: 55, //标识
						name: "barcode" + this.$kdup.index, //名称fkey
						value: obj[i].fnumber, //显示值
						rowvalue: obj[i],
						verify: "req", //必录fmustinput
						errMess: "请输入源单" + this.$kdup.index //非空提示
					}
				}, {
					placeholder: "请输入数量" + this.$kdup.index, //提示
					label: "数量" + this.$kdup.index, //标题
					index: this.$kdup.index,
					type: "number", //类型ffieldtype
					disabled: false, //是否启用
					maxlength: "-1", //输入长度
					property: {
						ffieldtype: 0,
						fishidden: 0
					}, //配置属性
					key: "fqty", //唯一标识
					rules: {
						flag: 999, //标识
						name: "fqty" + (i + 1), //名称fkey
						value: obj[i].fqty, //显示值
						rowvalue: obj[i],
						verify: "", //必录fmustinput
						errMess: "请输入数量" + this.$kdup.index //非空提示
					}
				}, {
					placeholder: "请输入辅助数量" + this.$kdup.index, //提示
					label: "辅助数量" + this.$kdup.index, //标题
					index: this.$kdup.index,
					type: "number", //类型ffieldtype
					disabled: true, //是否启用
					maxlength: "-1", //输入长度
					property: {
						fishidden: 0
					},
					key: "", //唯一标识
					rules: {
						del: true,
						flag: 1000, //标识
						name: "fauxunitqty" + this.$kdup.index, //名称fkey
						value: obj[i].fauxinvqt, //显示值
						rowvalue: obj[i],
						verify: "", //必录fmustinput
						errMess: "请输入辅助数量" + this.$kdup.index //非空提示
					}
				})
			}
		
		},
		methods: {
			//计算方式文本框输入值
			input:function(type) {
			if(type==1){
			if(this.$kdup.isEmpty(this.countqty)){
				this.countqty=1;
			}else{
				this.sumqty=parseFloat(this.countqty*this.smallqty);
			}
			}else{
			this.countqty=Math.ceil(this.sumqty/this.smallqty);
			}
			},
			//显示计算方式框
			show: function(index,qtyobj,srcqty) {
			this.obj=qtyobj;
			this.countqty=1;
			this.smallqty=srcqty;
			this.sumqty=srcqty;
			this.$refs.popup.show() 
            this.$kdup.index=index;
			},
			//关闭并且赋值
			close: function() {
			this.obj.rules.value=this.sumqty;
			if(this.formDate1[this.$kdup.index].rules.flag==999){
			this.formDate1[this.$kdup.index].rules.value=this.sumqty;
			this.formDate1[this.$kdup.index].rules.rowvalue.fqty=this.sumqty;
			}
			this.$refs.popup.close() 
			this.submit1();
			},
			//删除行
			remove: function(index) {
				for (var i = this.formDate1.length - 1; i >= 0; i--) {
					if (this.formDate1[i].index == index) {
						this.formDate1.splice(i, 1);
					}
				}
			},
			addrow: function() {
				this.$kdup.index++;
				this.formDate1.push({
					placeholder: "请输入源单" + this.$kdup.index, //提示
					label: "源单" + this.$kdup.index, //标题
					index: this.$kdup.index,
					type: "text", //类型ffieldtype
					disabled: true, //是否启用
					maxlength: "-1", //输入长度
					property: {
						fishidden: 0,
						ffieldtype: 55,
						ffilter: ""
					},
					key: "sourcekey", //唯一标识
					rules: {
						flag: 55, //标识
						name: "barcode" + this.$kdup.index, //名称fkey
						value: "", //显示值
						rowvalue: {},
						verify: "req", //必录fmustinput
						errMess: "请输入源单" + this.$kdup.index //非空提示
					}
				}, {
					placeholder: "请输入数量" + this.$kdup.index, //提示
					label: "数量" + this.$kdup.index, //标题
					index: this.$kdup.index,
					type: "number", //类型ffieldtype
					disabled: false, //是否启用
					maxlength: "-1", //输入长度
					property: {
						ffieldtype: 0,
						fishidden: 0
					}, //配置属性
					key: "fqty", //唯一标识
					rules: {
						flag: 999, //标识
						name: "fqty" + this.$kdup.index, //名称fkey
						value: "", //显示值
						rowvalue: "",
						verify: "", //必录fmustinput
						errMess: "请输入数量" + this.$kdup.index //非空提示
					}
				}, {
					placeholder: "请输入辅助数量" + this.$kdup.index, //提示
					label: "辅助数量" + this.$kdup.index, //标题
					index: this.$kdup.index,
					type: "number", //类型ffieldtype
					disabled: true, //是否启用
					maxlength: "-1", //输入长度
					property: {
						fishidden: 0
					},
					key: "", //唯一标识
					rules: {
						del: true,
						flag: 1000, //标识
						name: "fauxunitqty" + this.$kdup.index, //名称fkey
						value: "", //显示值
						rowvalue: "",
						verify: "", //必录fmustinput
						errMess: "请输入辅助数量" + this.$kdup.index //非空提示
					}
				})
			},
			//设置页签1表单的值
			inputVal1: function(data) {
				var data = JSON.parse(data);
				if (String(data.val) != "" && String(data.index) != "") {
					this.formDate1[data.index].rules.value = data.val;
					if (parseInt(data.index) % 3 == 0) {//源单
						this.formDate1[parseInt(data.index) + 1].rules.value = data.rowvalue.fqty;
						this.formDate1[parseInt(data.index) + 2].rules.value = data.rowvalue.fauxinvqty;
					}else if (parseInt(data.index) % 3 == 1){//数量
						this.formDate1[parseInt(data.index) - 1].rules.rowvalue.fqty=data.rowvalue.id;
					}else if (parseInt(data.index) % 3 == 2){//数量
						this.formDate1[parseInt(data.index) - 2].rules.rowvalue.fauxinvqty=data.rowvalue.id;
					}


					if (String(data.rowvalue) != "" || String(data.rowvalue) != null) {
						this.formDate1[data.index].rules.rowvalue = data.rowvalue;
						if (parseInt(data.index) % 3 == 0) {
							this.formDate1[parseInt(data.index) + 1].rules.rowvalue = {id:data.rowvalue.fqty};
							this.formDate1[parseInt(data.index) + 2].rules.rowvalue ={id:data.rowvalue.fauxinvqty};
						}

					}
				}
			},
			submit1: function() {
				var otherPra = {
					reqEmptyVal: true,
				}
				if (this.$vervify({
						formDate: this.formDate1,
						otherPra: otherPra
					})) {
					var sum = 0;
					var str = "";
					this.srcInfos = [];
					for (var i = 0; i < this.formDate1.length; i++) {
						if (this.formDate1[i].key == "sourcekey") {
							if (str.indexOf(this.formDate1[i].rules.value + ";") > -1) {
								uni.showToast({
									title: "源单重复请从新选择",
									icon: "none",
									duration: 3000
								});
								return;
							} else {
								str += this.formDate1[i].rules.value + ";"
							}
							this.srcInfos.push(this.formDate1[i])
						}
						if (this.formDate1[i].key == "fqty") {
							sum += parseFloat(this.formDate1[i].rules.value)
						}
					}
					//如果数量和上个页面的条码数量不一样的情况就报错
					if (isNaN(sum)) {
						sum = 0
					}
					// if (sum != this.qty) {
					// 	uni.showToast({
					// 		title: "数量不正确,条码数量" + this.qty + ",源单数量" + sum,
					// 		icon: "none",
					// 		duration: 3000
					// 	});
					// 	return;
					// }
					const pages = getCurrentPages(); //当前页
					const beforePage = pages[pages.length - 2]; //上个页面
					var data = {
						sum: sum,
						array: this.srcInfos
					}
					beforePage.$vm.srcInfosres(data);
					uni.navigateBack({
						delta: 1
					});
				}
			}
		}
	}
</script>

<style>
	page {
		background-color: #FFFFFF;
	}
	.inp{

		height: 40px;
	}
	.lab{
		float: left;
		position: relative;

	}
	.box {
		width: 100%;
		height: 84upx;
		display: flex;
		justify-content: center;
		flex-direction: row;
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;
		z-index: 999;
		
	}
	.buttonBox {
	margin-right: 2rpx;
	width: 50%;
	height: 84upx;
	color: white;
	display: flex;
	align-items: center;
	justify-content: center;
	bottom: 0;
	left: 0;
	right: 0;
	}
</style>
